import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import defAva from '@/assets/images/profile.jpg'

const useUserStore = defineStore(
  'user',
  {
    state: () => ({
      token: getToken(),
      id: '',
      name: '',
      avatar: '',
      roles: [],
      permissions: []
    }),
    actions: {
      // 登录
      login(userInfo) {
        const username = userInfo.username.trim()
        const password = userInfo.password
        const code = userInfo.code
        const uuid = userInfo.uuid
        return new Promise((resolve, reject) => {
          setToken('e1518a65-469e-4e50-9ee6-6b2a21f451b3')
          this.token = 'e1518a65-469e-4e50-9ee6-6b2a21f451b3'
          resolve()
          // login(username, password, code, uuid).then(res => {
          //   setToken(res.token)
          //   this.token = res.token
          //   resolve()
          // }).catch(error => {
          //   reject(error)
          // })
        })
      },
      // 获取用户信息
      getInfo() {
        console.log('有执行到获取用户数据嘛');
        let res = {
          "permissions": [
              "system:user:resetPwd",
              "system:post:list",
              "system:label:*",
              "system:menu:query",
              "system:dept:remove",
              "system:menu:list",
              "pm:project:member:delete",
              "pm:project:issue:task:export",
              "pm:project:member:update",
              "pm:project:model:list",
              "pm:project:model:delete",
              "system:dict:edit",
              "pm:project:delete",
              "pm:configurations:create",
              "system:user:add",
              "pm:problems:create",
              "pm:project:model:update",
              "pm:customers:update",
              "pm:project:milestone:list",
              "system:role:remove",
              "pm:project:software:list",
              "system:field:*",
              "pm:project:issue:bug:create",
              "pm:project:inspections:update",
              "system:dept:query",
              "pm:project:inspections:delete",
              "pm:project:issue:task:create",
              "system:dict:list",
              "pm:project:task:list",
              "pm:project:task:create",
              "pm:problems:delete",
              "pm:project:issue:bug:list",
              "pm:project:issue:bug:export",
              "pm:project:model:create",
              "pm:factories:delete",
              "pm:project:milestone:create",
              "system:dict:query",
              "pm:project:inspections:list",
              "pm:customers:delete",
              "pm:project:list",
              "pm:project:software:delete",
              "system:post:edit",
              "system:user:role",
              "pm:project:issue:task:list",
              "pm:project:update",
              "system:post:query",
              "system:post:remove",
              "pm:project:issue:bug:update",
              "pm:configurations:update",
              "system:user:remove",
              "system:menu:add",
              "system:role:list",
              "pm:project:task:update",
              "pm:configurations:list",
              "system:dict:remove",
              "pm:project:inspections:create",
              "pm:project:issue:task:delete",
              "system:user:edit",
              "system:role:edit",
              "system:dept:list",
              "pm:project:task:delete",
              "pm:factories:list",
              "pm:project:export",
              "pm:project:software:update",
              "pm:factories:create",
              "pm:project:member:create",
              "pm:project:milestone:update",
              "system:role:add",
              "pm:configurations:delete",
              "system:menu:remove",
              "system:dict:add",
              "pm:project:create",
              "pm:project:milestone:delete",
              "pm:factories:update",
              "pm:project:member:list",
              "system:dept:edit",
              "system:post:add",
              "system:user:list",
              "pm:project:issue:bug:delete",
              "pm:customers:list",
              "pm:problems:list",
              "pm:project:issue:task:update",
              "pm:customers:create",
              "pm:project:software:create",
              "pm:problems:update",
              "system:menu:edit",
              "system:dept:add"
          ],
          "roles": [
              "ceshi测试"
          ],
          "user": {
              "createTime": "2024-04-30 09:02:02",
              "updateTime": "2024-05-16 17:09:46",
              "userId": "bJVSQEuEQFtftCqZWC2cwY",
              "deptId": "7bb316224a79be9fbea08046a413ae70",
              "userName": "amy",
              "nickName": "amy",
              "email": "727804606@qq.com",
              "phonenumber": "18512110066",
              "sex": "1",
              "avatar": "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80",
              "password": "$2a$10$wQYuxhh5W2O6OHLcRokkaOtloiteztKfzeUunZWG9XJ0NHUMm9vRK",
              "status": "0",
              "isDeleted": "0",
              "loginIp": "222.209.68.117",
              "loginDate": "2024-07-01 15:42:42",
              "dept": {
                  "deptId": "7bb316224a79be9fbea08046a413ae70",
                  "parentId": "100",
                  "ancestors": "0,100",
                  "deptName": "上海分公司",
                  "orderNum": 5,
                  "leader": "amy",
                  "email": "727804606@qq.com",
                  "status": "0",
                  "parentName": "洪朴科技",
                  "children": []
              },
              "roles": [
                  {
                      "createBy": "cabits@hongpucorp.com",
                      "createTime": "2024-04-30 10:59:00",
                      "updateBy": "cabits@hongpucorp.com",
                      "updateTime": "2024-04-30 15:53:47",
                      "roleId": "e6c64ab777ffce4a4cef1046963f0ddc",
                      "roleName": "测试角色",
                      "roleKey": "ceshi测试",
                      "roleSort": 2,
                      "dataScope": "4",
                      "menuCheckStrictly": true,
                      "deptCheckStrictly": false,
                      "status": "0",
                      "isDeleted": "0",
                      "flag": false,
                      "permissions": [
                          "system:user:resetPwd",
                          "system:post:list",
                          "system:label:*",
                          "system:menu:query",
                          "system:dept:remove",
                          "system:menu:list",
                          "pm:project:member:delete",
                          "pm:project:issue:task:export",
                          "pm:project:member:update",
                          "pm:project:model:list",
                          "pm:project:model:delete",
                          "system:dict:edit",
                          "pm:project:delete",
                          "pm:configurations:create",
                          "system:user:add",
                          "pm:problems:create",
                          "pm:project:model:update",
                          "pm:customers:update",
                          "pm:project:milestone:list",
                          "system:role:remove",
                          "pm:project:software:list",
                          "system:field:*",
                          "pm:project:issue:bug:create",
                          "pm:project:inspections:update",
                          "system:dept:query",
                          "pm:project:inspections:delete",
                          "pm:project:issue:task:create",
                          "system:dict:list",
                          "pm:project:task:list",
                          "pm:project:task:create",
                          "pm:problems:delete",
                          "pm:project:issue:bug:list",
                          "pm:project:issue:bug:export",
                          "pm:project:model:create",
                          "pm:factories:delete",
                          "pm:project:milestone:create",
                          "system:dict:query",
                          "pm:project:inspections:list",
                          "pm:customers:delete",
                          "pm:project:list",
                          "pm:project:software:delete",
                          "system:post:edit",
                          "system:user:role",
                          "pm:project:issue:task:list",
                          "pm:project:update",
                          "system:post:query",
                          "system:post:remove",
                          "pm:project:issue:bug:update",
                          "pm:configurations:update",
                          "system:user:remove",
                          "system:menu:add",
                          "system:role:list",
                          "pm:project:task:update",
                          "pm:configurations:list",
                          "system:dict:remove",
                          "pm:project:inspections:create",
                          "pm:project:issue:task:delete",
                          "system:user:edit",
                          "system:role:edit",
                          "system:dept:list",
                          "pm:project:task:delete",
                          "pm:factories:list",
                          "pm:project:export",
                          "pm:project:software:update",
                          "pm:factories:create",
                          "pm:project:member:create",
                          "pm:project:milestone:update",
                          "system:role:add",
                          "pm:configurations:delete",
                          "system:menu:remove",
                          "system:dict:add",
                          "pm:project:create",
                          "pm:project:milestone:delete",
                          "pm:factories:update",
                          "pm:project:member:list",
                          "system:dept:edit",
                          "system:post:add",
                          "system:user:list",
                          "pm:project:issue:bug:delete",
                          "pm:customers:list",
                          "pm:problems:list",
                          "pm:project:issue:task:update",
                          "pm:customers:create",
                          "pm:project:software:create",
                          "pm:problems:update",
                          "system:menu:edit",
                          "system:dept:add"
                      ],
                      "admin": false
                  }
              ],
              "admin": false
          }
        }
      return new Promise((resolve, reject) => {
          const user = res.user
          const avatar = (user.avatar == "" || user.avatar == null) ? defAva : user.avatar;

          if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            this.roles = res.roles
            this.permissions = res.permissions
          } else {
            this.roles = ['ROLE_DEFAULT']
          }
          this.id = user.userId
          this.name = user.userName
          this.avatar = avatar
          resolve(res)
        }).catch(error => {
          reject(error)
      })
        // return new Promise((resolve, reject) => {
        //   getInfo().then(res => {
        //     const user = res.user
        //     const avatar = (user.avatar == "" || user.avatar == null) ? defAva : import.meta.env.VITE_APP_BASE_API + user.avatar;

        //     if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
        //       this.roles = res.roles
        //       this.permissions = res.permissions
        //     } else {
        //       this.roles = ['ROLE_DEFAULT']
        //     }
        //     this.id = user.userId
        //     this.name = user.userName
        //     this.avatar = avatar
        //     resolve(res)
        //   }).catch(error => {
        //     reject(error)
        //   })
        // })
      },
      // 退出系统
      logOut() {
        return new Promise((resolve, reject) => {
          logout(this.token).then(() => {
            this.token = ''
            this.roles = []
            this.permissions = []
            removeToken()
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      }
    }
  })

export default useUserStore
